package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.UserDao;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.User;
import com.woniuxy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    UserDao userDao;

    @Override
    public ResponseEntity findAll(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<User> list = userDao.findAll();
        PageInfo<User> pageInfo = PageInfo.of(list);
        return new ResponseEntity(200,"查询用户成功",pageInfo);
    }

    @Override
    public ResponseEntity addOne(User user) {
        if(userDao.addOne(user) > 0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity updateOne(User user) {
        if(userDao.updateOne(user) > 0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity deleteOne(int id) {
        if(userDao.deleteOne(id) > 0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity findByContext(User user) {
        List<User> list = userDao.findByContext(user);
        return new ResponseEntity(200,"查询指定内容成功",list);
    }

    @Override
    public User findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    @Override
    public User login(String username, String password) {
        return userDao.login(username,password);
    }

    @Override
    public ResponseEntity updatePassword(Object obj) {
        if(userDao.updatePassword(obj) > 0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity batchDelete(int[] ids) {
        if(userDao.batchDelete(ids) > 0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity getPassword(int id) {
        return new ResponseEntity(200,"获取密码成功",userDao.getPassword(id));
    }

    @Override
    public ResponseEntity findByNo(String no) {
        String user_no = userDao.findByNo(no);
        if (user_no!=null){
            return new ResponseEntity(200,"成功",user_no);
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity findByName(String username) {
        String name = userDao.findByName(username);
        if (name!=null){
            return new ResponseEntity(200,"成功",name);
        }
        return ResponseEntity.Fail;
    }

}
